package indexer

import com.mongodb.MongoClient
import com.mongodb.client.model.Filters
import com.mongodb.client.model.UpdateOptions
import us.codecraft.webmagic.Request
import us.codecraft.webmagic.SpiderListener
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger
import kotlin.concurrent.thread

import com.mongodb.client.model.Updates.*

class StatusListener(val mongoClient: MongoClient,val spiderId:String) : SpiderListener {

    private val sussCount = AtomicInteger()
    private val errCount = AtomicInteger()
    private  @Volatile  var url:String? = null

    init {
        thread(isDaemon = true) {
            var database = mongoClient.getDatabase("book")
            var collection = database.getCollection("spiders")
            while (true){

                collection.updateOne(Filters.eq("_id", spiderId),
                        combine(set("count",sussCount.get() + errCount.get())
                                ,set("success",sussCount.get())
                                ,set("error",errCount.get())
                                ,set("lastReqUrl",url)),
                                UpdateOptions().upsert(true))
                TimeUnit.SECONDS.sleep(10)
            }
        }
    }

    override fun onSuccess(request: Request) {
        sussCount.incrementAndGet()
        url = request.url
    }

    override fun onError(request: Request?) {
        errCount.incrementAndGet()
    }
}